package com.ham.mud.state.edit;

import com.ham.mud.Colors;
import com.ham.mud.ServerConnection;
import com.ham.mud.characters.mobs.Mob;
import com.ham.mud.characters.mobs.MobService;
import com.ham.mud.configuration.FilePaths;
import com.ham.mud.items.Item;
import com.ham.mud.items.ItemService;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;
import com.ham.mud.state.ConnectionState;

import java.io.File;
import java.util.List;

/**
 * Created by hlucas on Jul 18, 2011 at 11:14:10 AM
 */
public class EditRoomState extends ConnectionState {
    boolean editingTitle = false;
    boolean editingDesc = false;
    boolean editingItems = false;
    boolean editingMobs = false;
    boolean editingExits = false;

    public EditRoomState(ServerConnection connection) {
        super(connection);
        connection.println("Editing room.");
        editWhat();
    }

    @Override
    public void processInput(String line) {
        if(line.equals("exit")) {
            connection.println("No longer editing room.");
            connection.changeState(new EditState(connection));
            return;
        }

        Room room = RoomService.getRoom(connection);

        if(editingTitle) {
            editingTitle = false;
            connection.println("Title is now, \"" + line + "\"");
            RoomService.updateTitle(room, line);
            editWhat();
        } else if(editingDesc) {
            editingDesc = false;
            connection.println("Description is now, \"" + line + "\"");
            RoomService.updateDescription(room, line);
            editWhat();
        } else if(editingItems){
            editingItems = false;
            String[] split = line.split(" ");
            if(line.startsWith("add")) {
                RoomService.addItem(room, split[1]);
                connection.println("Item added.");
            } else if(line.startsWith("remove")) {  
                RoomService.removeItem(room, split[1]);
                connection.println("Item removed.");
            }
            editWhat();
        } else if(editingMobs){
            editingMobs = false;
            String[] split = line.split(" ");
            if(line.startsWith("add")) {
                RoomService.addMob(room, split[1]);
                connection.println("Mob added.");
            } else if(line.startsWith("remove")) {
                RoomService.removeMob(room, split[1]);
                connection.println("Mob removed.");
            }

            editWhat();
        } else if(editingExits){
            editingExits = false;
            String[] split = line.split(" ");
            if(line.startsWith("add")) {
                RoomService.addExit(room, split[1]);
                connection.println("Exit added.");
            } else if(line.startsWith("remove")) {
                RoomService.removeExit(room, split[1]);
                connection.println("Exit removed.");
            }

            editWhat();
        } else {
            if(line.equals("title")) {
                editingTitle = true;
                connection.println(Colors.NEW_LINE + "Current title:" + Colors.NEW_LINE + room.getName());
                connection.print("New title: ");
            } else if(line.equals("desc")) {
                editingDesc = true;
                connection.println(Colors.NEW_LINE + "Current desc:" + Colors.NEW_LINE + room.getDescription());
                connection.print("New desc: ");
            } else if(line.equals("items")) {
                editingItems = true;
                connection.println(Colors.NEW_LINE + "Current items:");
                List<Item> items = room.getItems();
                for(Item item : items) {
                    connection.println(item.getFileName() + " - " + item.getShortDescription());
                }
                connection.println();
                connection.println("Available items:");
                printAvailableItems();

                connection.println();
                connection.print("add <item> or remove <item>: ");
            } else if(line.equals("mobs")){
                editingMobs = true;
                connection.println(Colors.NEW_LINE + "Current mobs:");
                List<Mob> mobs = room.getMobs();
                for(Mob mob : mobs) {
                    connection.println(mob.getFileName() + " - " + mob.getShortDescription());
                }

                connection.println();
                connection.println("Available mobs:");
                printAvailableMobs(room.getZone());
                connection.println();
                connection.print("add <mob> or remove <mob>: ");
            } else if(line.equals("exits")){
                editingExits = true;
                connection.println(Colors.NEW_LINE + "Current exits:");
                String[] exits = room.getExits();
                for(String exit : exits) {
                    connection.println(exit);
                }
                connection.println();
                connection.print("add <exit> or remove <exit>: ");
            } else {
                editWhat();
            }
        }
    }

    private void printAvailableItems() {
        File itemsDir = new File(FilePaths.getItemsFilePath());
        File[] files = itemsDir.listFiles();
        for(File file : files) {
            if(file.isDirectory()) continue; //stupid .svn directory
            Item item = ItemService.getItem(file.getName().split("\\.")[0]);
            connection.println(item.getFileName() + " - " + item.getShortDescription());
        }
    }

    private void printAvailableMobs(String zone) {
        File mobsDir = new File(FilePaths.getMobsFilePath(zone));
        File[] files = mobsDir.listFiles();
        for(File file : files) {
            if(file.isDirectory()) continue; //stupid .svn directory
            Mob mob = MobService.getMob(zone, file.getName().split("\\.")[0]);
            connection.println(mob.getFileName() + " - " + mob.getShortDescription());
        }
    }

    private void editWhat() {
        connection.print("Edit what? title, desc, items, mobs, exits: ");
    }

    @Override
    public void tick() {}
}
